﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GenerationCode.CSClasses
{
    class TableObjectResult
    {
        private Utils.Table table;
        private string projectName;
        private string coder;
        private string email;
        private string objectClass;

        public TableObjectResult(Utils.Table tb, string projectName, string coder, string email)
        {
            this.table = tb;
            this.projectName = projectName;
            this.coder = coder;
            this.email = email;
        }
        private string SetHeader()
        {
            string header = string.Format(@"/*
            Author : {0}
            Email  : {1}
            Date   : {2}                        
            */

             using System;
            using System.Collections.Generic; 
            using System.Text;  
            namespace {3}{5}
             /// <summary>
                    /// Define {4}Result object. This object used to contain data that returned from data layer.
                /// </summary>
                public class {4}Result {5}",
                this.coder, this.email, DateTime.Now.ToShortDateString(),Utils.Utility.BusinessObjectNamespace, this.table.Name, "{");
            return header;
        }

        private string SetProperties()
        {
            string properties = string.Format(@"

		 #region properties");

            foreach (Utils.Column col in this.table.Columns)
            {
                properties += string.Format(@"
          /// <summary>
            /// Define {1} Property.
            /// </summary>
		public {0} {1} {3}
			get {3} return this.{2}; {4}
			set {3} this.{2} = value; {4}
		{4}",
                    col.DataType, Utils.Utility.ConvertToPropertyName(col.Name), col.Name, "{", "}");
            }
            /*if (this.table.IsMultilanguage)
            {
                properties += @"
              /// <summary>
            /// Define " + table.Name + Utils.Utility.MultilanguageTableName + Utils.Utility.MultilanguageTableNameList + @" Property
            /// </summary>
            public List<" + table.Name + Utils.Utility.MultilanguageTableName + "> " + table.Name + Utils.Utility.MultilanguageTableName + Utils.Utility.MultilanguageTableNameList + @"
            {
                get { return this.lst" + table.Name + Utils.Utility.MultilanguageTableName + @";}
			    set { this.lst" + table.Name + Utils.Utility.MultilanguageTableName + @"  = value;}
            }
		#endregion properties";
            }
            else
            {
                properties += @"
              #endregion properties";
            }*/

            properties += @"
              #endregion properties";
            //properties += "\r\n #endregion properties";
            return properties;
        }

        private string SetFields()
        {
            string fields = @"
		#region fields";
            //set fields
            foreach (Utils.Column col in this.table.Columns)
            {
                fields += string.Format(@"
		private {0} {1}; ", col.DataType, col.Name);
            }
          /*  if (table.IsMultilanguage)
               fields += "\r\n private List<" + table.Name + Utils.Utility.MultilanguageTableName + "> lst" + table.Name + Utils.Utility.MultilanguageTableName + ";\r\n";
            */
            fields += "\r\n #endregion fields";


            return fields;
        }


        private string SetFooter()
        {
            string footer = @"
	}
}";
            return footer;
        }
        private string SetConstructor()
        {
            string constructor = @"
		
		#region constructor";
            //first constructor
            constructor += string.Format(@"
        /// <summary>
            /// Define default constructor for {0}Result Object.
            /// </summary>
		public {0} (){1}", this.table.Name + "Result", "{");
            foreach (Utils.Column col in this.table.Columns)
            {

                constructor += string.Format(@"
			this.{0} = {1};", col.Name, Utils.Utility.InitializeDataType(col.DataType));

            }
           // constructor += "\r\nthis.totalRecord = 0;";
           /* if(table.IsMultilanguage)
                constructor += @"
            this.lst" + table.Name + Utils.Utility.MultilanguageTableName + @"=null;
		}";
            else
                constructor += @"            
		}";*/
            constructor += @"            
		}";
            //second constructor
            constructor += string.Format(@"
 /// <summary>
            /// Define constructor for {0}Result Object.
            /// </summary>
		public {0} ( ", this.table.Name + "Result");
            foreach (Utils.Column col in this.table.Columns)
            {

                constructor += string.Format(@"{0} {1}, 
					", col.DataType, col.Name);

            }
            constructor = constructor.TrimEnd();
            constructor = constructor.Remove(constructor.Length - 1, 1) + " ) {";
            foreach (Utils.Column col in this.table.Columns)
            {

                constructor += string.Format(@"
			this.{0} = {0};", col.Name);

            }
           // constructor += "\r\nthis.totalRecord = 0;";
            /*if(table.IsMultilanguage)
                constructor += @"
             this.lst" + table.Name + Utils.Utility.MultilanguageTableName + @"=null;
		}";
            else
                constructor += @"             
		}";*/
            constructor += @"             
		}";
            
            //close constructor
            constructor += @"
		#endregion constructor";

            return constructor;

        }

        public string ObjectClass
        {
            get
            {
                this.objectClass = this.SetHeader() + this.SetFields() + this.SetProperties() + this.SetConstructor() + this.SetFooter();
                return this.objectClass;
            }
        }


    }
}
